var app = new Vue({
	el: '#app',
	data: {
		userMsg: {
			isLogin: false,
			id: null,
			account: null,
			name: null,
			headimg: null
		},
		newsList: [],
		moduleList: [],
		module_id: -1
	},
	computed: {
		showList: function(){
			var ret = [];
			if (this.module_id == -1) {
				ret = this.newsList;
			} else {
				for (var i = 0; i < this.newsList.length; i++) {
					if (this.newsList[i].module_id == this.module_id) {
						ret.push(this.newsList[i])
					}
				}
			}
			return ret;
		},
		showName: function(){
			var name = '全部';
			for (var i = 0; i < this.moduleList.length; i++) {
				if (this.moduleList[i].id == this.module_id) {
					name = this.moduleList[i].name;
					break;
				}
			}
			return name;
		}
	},
	methods: {
		logout: function(){
			var self = this;

			swal({ 
				title: "确定退出吗？", 
				type: "warning",
				showCancelButton: true, 
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "确定", 
  				cancelButtonText: "取消"
			},function(){
				$.post("/api/logout",{},function(data,status){
					if (data.success = "ok") {
						self.getLoginStatus();
					}
				});
			});

		},
		getLoginStatus: function(){
			var self = this;
			$.get('/api/isLogin',function(data,status){
				if (data.success == "ok") {
					self.userMsg.isLogin = true;
					self.userMsg.name = data.result.name;
					self.userMsg.id = data.result.id;
					self.userMsg.account = data.result.account;
					self.userMsg.headimg = data.result.headimg;
				} else {
					self.userMsg.isLogin = false;
				}
			})
		},
		getNews: function(){
			var self = this;
			$.post('/api/getNewsList',function(data,status){
				if (data.success == "ok") {
					self.newsList = data.result;
				} else {
					swal("网络错误","请重试","error");
				}
			})
		},
		getModule: function(){
			var self = this;
			$.post('/api/getModule',function(data,status){
				if (data.success == "ok") {
					self.moduleList = data.result;
				} else {
					swal("网络错误","请重试","error");
				}
			})

		},
		getQueryString: function(name) { 
			var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); 
			var r = window.location.search.substr(1).match(reg); 
			if (r != null) return unescape(r[2]); return null; 
		}
	},
	mounted: function(){
		this.getLoginStatus();
		this.getNews();
		this.getModule();
		if (this.getQueryString("module") != null) {
			this.module_id = this.getQueryString("module")
		}
	}
})
